﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;




static class LogUpload
{
    static Socket m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    static string m_project_json;

    public static void Init(string address, string project_json)
    {
        m_project_json = project_json;
        try
        {
            string dns = "logstash.h3d.com.cn";
            int port = 32194;
            IPAddress ip;
            string[] ss = address.Split(':');
            if (ss.Length == 2)
            {
                dns = ss[0];
                port = int.Parse(ss[1]);
            }
            if (!IPAddress.TryParse(dns, out ip))
            {
                IPHostEntry host = Dns.GetHostEntry(dns);
                ip = host.AddressList[0];
            }
            m_socket.Blocking = false;
            m_socket.Connect(ip, port);
        }
        catch (Exception ex)
        {
            //??
        }
    }

    public static void Upload(string msg)
    {
        if (string.IsNullOrEmpty(m_project_json))
        {
            return;
        }

        string s = string.Format("{{\"head\":{0},\"message\":\"{1}\"}}", m_project_json, msg);
        //string s = "{\"project\": \"AAA\", \"message\": \"blablabla...\"}";
        m_socket.Send(Encoding.UTF8.GetBytes(s));
    }


}

